package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {


    @Autowired
    private WorkerCertificationAuditService workerCertificationAuditService;

    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }


    /**
     * 服务端提交认证申请
     * @param dto
     */
    @Override
    @Transactional
    public void workerCertificationAudit(WorkerCertificationAuditAddReqDTO dto) {
        WorkerCertification workerCertification = BeanUtil.toBean(dto, WorkerCertification.class);
        LambdaQueryWrapper<WorkerCertification> queryWrapper = Wrappers.<WorkerCertification>lambdaQuery().eq(WorkerCertification::getIdCardNo, workerCertification.getIdCardNo());
        WorkerCertification workerCertification1 = this.getOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(workerCertification1)){
            throw new CommonException("不能重复进行身份认证");
        }
        workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        boolean save = this.save(workerCertification);
        if (!save){
            throw new DBException("操作失败");
        }
        WorkerCertificationAuditResDTO workerCertificationAudit = new WorkerCertificationAuditResDTO();
        BeanUtil.copyProperties(dto, workerCertificationAudit);
        workerCertificationAudit.setServeProviderId(UserContext.currentUserId());
        workerCertificationAudit.setAuditStatus(CertificationStatusEnum.INIT.getStatus());
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        workerCertificationAudit.setAuditorName(currentUserInfo.getName());
        workerCertificationAudit.setAuditorId(currentUserInfo.getId());
        LambdaQueryWrapper<WorkerCertificationAuditResDTO> queryWrapper1 = Wrappers.<WorkerCertificationAuditResDTO>lambdaQuery().eq(WorkerCertificationAuditResDTO::getIdCardNo, dto.getIdCardNo());
        WorkerCertificationAuditResDTO auditServiceOne = workerCertificationAuditService.getOne(queryWrapper1);
        if (ObjectUtil.isNotEmpty(auditServiceOne)){
            throw new CommonException("用户已存在");
        }
        boolean save1 = workerCertificationAuditService.save(workerCertificationAudit);
        if (!save1){
            throw new DBException("操作失败");
        }
    }

    /**
     * 审核服务人员认证分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult<WorkerCertificationAuditResDTO> pageQuery(WorkerCertificationAuditPageQueryReqDTO dto) {
        Page<WorkerCertificationAuditResDTO> page = PageUtils.parsePageQuery(dto, WorkerCertificationAuditResDTO.class);
        LambdaQueryWrapper<WorkerCertificationAuditResDTO> queryWrapper = Wrappers.<WorkerCertificationAuditResDTO>lambdaQuery().like(ObjectUtil.isNotEmpty(dto.getName()),WorkerCertificationAuditResDTO::getName, dto.getName())
                .eq(ObjectUtil.isNotEmpty(dto.getAuditStatus()), WorkerCertificationAuditResDTO::getAuditStatus, dto.getAuditStatus())
                .eq(ObjectUtil.isNotEmpty(dto.getCertificationStatus()), WorkerCertificationAuditResDTO::getCertificationStatus, dto.getCertificationStatus())
                .like(ObjectUtil.isNotEmpty(dto.getIdCardNo()),WorkerCertificationAuditResDTO::getIdCardNo, dto.getIdCardNo());
        Page<WorkerCertificationAuditResDTO> page1 = workerCertificationAuditService.page(page, queryWrapper);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page1.getTotal());
        pageResult.setList(page1.getRecords());
        pageResult.setPages(page1.getPages());
        return pageResult;
    }

    /**
     * 查询最新的驳回原因
     * @return
     */
    @Override
    public RejectReasonResDTO rejectReason() {
        LambdaQueryWrapper<WorkerCertificationAuditResDTO> queryWrapper = Wrappers.<WorkerCertificationAuditResDTO>lambdaQuery().eq(WorkerCertificationAuditResDTO::getServeProviderId, UserContext.currentUserId()).orderByDesc(WorkerCertificationAuditResDTO::getCreateTime).last("limit 1");
        List<WorkerCertificationAuditResDTO> list = workerCertificationAuditService.list(queryWrapper);
        WorkerCertificationAuditResDTO workerCertificationAuditResDTO=null;
        for (WorkerCertificationAuditResDTO workerCertificationAuditResDTO1 : list) {
            workerCertificationAuditResDTO=workerCertificationAuditResDTO1;
        }
        if (ObjectUtil.isEmpty(workerCertificationAuditResDTO)){
            throw new CommonException("服务人员不存在！");
        }
        return new RejectReasonResDTO(workerCertificationAuditResDTO.getRejectReason());
    }


}
